package com.lengyue.imakiba.logic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.lengyue.imakiba.data.Episode;

public class EpisodeLogic {
	/**
	 * 根据动漫Id查询剧集，根据num排序
	 * @param animeId 动漫Id
	 * @return 符合条件的剧集
	 */
	public static List<Episode> getEpisodeByAnimeId(int animeId)
	{
		return Episode.dao.find("select * from episode where anime_id = ? order by num",animeId);
	}
	/**
	 * 根据动漫Id、剧集号查询一条数据
	 * @param animeId 动漫Id
	 * @param num 剧集号
	 * @return 符合条件的剧集
	 */
	public static Episode getEpisodeByAnimeIdAndNum(int animeId,int num)
	{
		return Episode.dao.findFirst("select * from episode where anime_id = ? and num = ?", animeId,num);
	}
	/**
	 * 根据动漫Id分页查询剧集并按num倒序排列
	 * @param page 页号
	 * @param length 每页数据个数
	 * @param animeId 动漫Id
	 * @return 符合条件的分页的剧集
	 */
	public static Page<Episode> getEpisodeByAnimeSortByNumDescAndPagenate(int page,int length,int animeId)
	{
		return Episode.dao.paginate(page, length,"select * ","from episode where anime_id = ? order by num desc",animeId);
	}
	/**
	 * 取动漫视频路径对应的集数，筛选出url不为空的数据，返回url顺序按参数urlNames顺序排序
	 * @param episodes 动漫数据
	 * @param urlNames 需要的url的名称
	 * @return 动漫对应的集数数据url名称级对应的动漫num、name、episodeId
	 */
	public static LinkedHashMap<String,ArrayList<HashMap<String,String>>> getEpisodeUrls(List<Episode> episodes,String... urlNames)
	{
		LinkedHashMap<String,ArrayList<HashMap<String,String>>> episodeUrls = new LinkedHashMap<String,ArrayList<HashMap<String,String>>>();
		for(String urlName : urlNames)
		{
			ArrayList<HashMap<String,String>> urls = new ArrayList<HashMap<String,String>>();
			for(Episode e : episodes)
			{
				if(e.getStr(urlName)!=null&&!"".equals(e.getStr(urlName)))
				{
					HashMap<String,String> urlMap = new HashMap<String,String>();
					urlMap.put("num", e.getInt("num").toString());
					urlMap.put("name", e.getStr("name"));
					urlMap.put("episodeId", e.getInt("id").toString());
					urls.add(urlMap);
				}
				episodeUrls.put(urlName, urls);
			}
		}
		return episodeUrls;
	}
}
